package com.hry.store.controller;

import com.hry.store.common.ServerResponse;
import com.hry.store.pojo.Order;
import com.hry.store.pojo.SecondCategory;
import com.hry.store.pojo.TopCategory;
import com.hry.store.service.OrderService;
import com.hry.store.service.ProductVoService;
import com.hry.store.service.SecondCategoryService;
import com.hry.store.service.TopCategoryService;
import com.hry.store.vo.HotProductVo;
import com.hry.store.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private SecondCategoryService secondCategoryService;
    @Autowired
    private TopCategoryService topCategoryService;
    @Autowired
    private ProductVoService productVoService;
    @Autowired
    private OrderService orderService;

    /**
     * 访问首页
     *
     * @return
     */
    @RequestMapping(value = "page/index")
    public String pageIndex(ModelMap modelMap) {
        // 查询出一级分类关联查询出二级分类
        List<TopCategory> topCategoryList = topCategoryService.selectAllTopCategory();
        modelMap.put("topCategoryList", topCategoryList);
        // 从redis中 查询出 明星单品(已经下过单的 order)
        List<Order> orderList = orderService.getStartProduct();

        modelMap.put("orderList", orderList);

        // 查询出家用电器
        List<ProductVo> productVos = productVoService.getByTopCategoryId();
        modelMap.put("productVos", productVos);
        // 首页轮播图 图片
        List<HotProductVo> hotProductVo = productVoService.getHotProduct();
        modelMap.put("hotProductVo", hotProductVo);

        return "index";
    }

    /**
     * 根据一级分类查询二级分类
     *
     * @param topCategoryId
     * @return
     */
    @RequestMapping("category/getSecondCategory")
    @ResponseBody
    public ServerResponse selectByTopCategoryId(@RequestParam(value = "topCategoryId") Integer topCategoryId) {
        // 根据一级分类id查询出二级分类
        List<SecondCategory> secondCategoryList = secondCategoryService.selectByTopCategoryId(topCategoryId);
        return ServerResponse.createBySuccess(secondCategoryList);
    }

}
